package com.tg.app.util;

import android.content.Context;

import com.tange.module.device.feature.DeviceFeatureObjectBox;
import com.tg.app.BuildConfig;

import com.tg.app.bean.DevicePresetPoints;
import com.tg.app.bean.DevicePresetPoints_;
import com.tg.app.bean.DeviceReboot;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.app.bean.FirmwareUpgrade;
import com.tg.app.bean.LocalThumbnail;
import com.tg.data.bean.MessageFilter;
import com.tg.app.bean.MyObjectBox;
import com.tg.app.bean.WifiItem;
import com.tg.app.bean.WifiItem_;
import com.tange.base.toolkit.FileUtil;
import com.tg.data.bean.DeviceItem;
import com.tg.data.helper.ObjectBoxHelper;
import com.tg.data.http.entity.GetOsdRespBean;
import com.tg.data.http.entity.OsdItem;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.android.AndroidObjectBrowser;

/**
 * Describe:
 * Created by Rance Rang on 2019/5/10.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class ObjectBoxUtil {
    private static final String TAG = "ObjectBoxUtil";
    private static BoxStore boxStore;

    public static void init(Context context) {
        try {
            boxStoreBuilder(context);
        } catch (Exception e) {
            String path = ObjectBoxHelper.getAndroidFilesDir(context);
            FileUtil.deleteDirectory(path);
            boxStoreBuilder(context);
        }
        ObjectBoxHelper.init(context);
        DeviceFeatureObjectBox.init(context);
        if (BuildConfig.DEBUG) {
            new AndroidObjectBrowser(boxStore).start(context);
            TGLog.d(TAG, String.format("Using ObjectBox %s (%s)",
                    BoxStore.getVersion(), BoxStore.getVersionNative()));
        }
    }
    private  static void boxStoreBuilder(Context context){
        boxStore = MyObjectBox.builder()
                .androidContext(context)
                .maxReaders(256)
                .build();
    }

    public static Box<DeviceItem> getDeviceItem() {
        return ObjectBoxHelper.getDeviceItem();
    }

    public static Box<WifiItem> getWifiItem() {
        if (boxStore == null) return null;
        return boxStore.boxFor(WifiItem.class);
    }

    public static String getPassword(String name) {
        Box<WifiItem> box = ObjectBoxUtil.getWifiItem();
        if (box != null) {
            WifiItem wifiItem = box.query().equal(WifiItem_.uuid, name).build().findFirst();
            if (wifiItem == null) {
                return "";
            }
            return wifiItem.password;
        }
        return "";
    }

    public static Box<FirmwareUpgrade> getFirmwareUpgrade() {
        if (boxStore == null) return null;
        return boxStore.boxFor(FirmwareUpgrade.class);
    }

    public static Box<GetOsdRespBean> getGetOsdRespBean() {
        return ObjectBoxHelper.getGetOsdRespBean();
    }

    public static Box<OsdItem> getOsdItem() {
        return ObjectBoxHelper.getOsdItem();
    }

    public static Box<DeviceSettingsInfo> getDeviceSettingsInfo() {
        return ObjectBoxHelper.getDeviceSettingsInfo();
    }

    public static Box<DeviceReboot> getDeviceReboot() {
        if (boxStore == null) return null;
        return boxStore.boxFor(DeviceReboot.class);
    }
    public static Box<MessageFilter> getMessageFilter() {
        return ObjectBoxHelper.getMessageFilter();
    }

    public static Box<LocalThumbnail> getLocalThumbnail() {
        if (boxStore == null) return null;
        return boxStore.boxFor(LocalThumbnail.class);
    }
    public static Box<DevicePresetPoints> getDevicePresetPoints() {
        if (boxStore == null) return null;
        return boxStore.boxFor(DevicePresetPoints.class);
    }

    public static void  saveDevicePresetPoints(List<DevicePresetPoints> devicePresetPoints,String uuid){
        Box<DevicePresetPoints> box = getDevicePresetPoints();
        if (box!=null&&devicePresetPoints!=null){
            List<DevicePresetPoints> points = box.query().equal(DevicePresetPoints_.uuid, uuid).build().find();
            box.remove(points);
            box.put(devicePresetPoints);
        }
    }


    public static List<DevicePresetPoints>  getDevicePresetPoints(String uuid){
        Box<DevicePresetPoints> box = getDevicePresetPoints();
        if (box!=null){
            return box.query().equal(DevicePresetPoints_.uuid, uuid).build().find();
        }
        return  null;
    }

    public static void updateSodRespBeanBox(GetOsdRespBean getOsdRespBean){
        ObjectBoxHelper.updateSodRespBeanBox(getOsdRespBean);
    }

}
